
import 'package:flutter_download_plugin/utils/video/m3u8_label.dart';

class M3u8LabelUtils {
  ///没有就新增有就修改对应的标签
  static String addOrUpdateM3u8Label(String content, String label, String value) {
    ///按照换行符分割成列表
    List<String> lines = content.split('\n');

    //去除相关标签
    for (int i = 0; i < lines.length; i++) {
      if (lines[i].startsWith(label)) {
        lines.removeAt(i--);
      }
    }
    int segmentIndex = findEXTINFIndex(lines);
    lines.insert(segmentIndex, "${M3u8Label.EXT_MAP}:URI=\"$value\"");
    //转换为字符串
    String result = lines.join('\n');
    return result;
  }

  ///找到分片信息的行数
  static int findEXTINFIndex(List<String> lines) {
    for (int i = 0; i < lines.length; i++) {
      if (lines[i].startsWith(M3u8Label.EXT_INFO)) {
        return i;
      }
    }
    return -1;
  }
}
